package com.benew.ntt.evalsdk

import android.os.Bundle
import android.util.Log
import android.widget.RadioButton
import androidx.appcompat.app.AppCompatActivity
import com.benew.ntt.evalsdk.databinding.ActivityMainBinding
import com.benew.ntt.evaluate.constant.EvalType
import com.benew.ntt.evaluate.sdk.*
import com.blankj.utilcode.util.ToastUtils

class MainActivity : AppCompatActivity() {
    val TAG = javaClass.simpleName
    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        setSupportActionBar(binding.toolbar)
        binding.editText.setText("你好")
        binding.rbtnCn.isChecked = true
//        binding.rbtnEn.isChecked = true

//        NLogger.d(TAG, "打印奇怪怪怪的空格：", "正常空格= ", "NBSP= ", "u00A0=\u00A0", "ㅤ=ㅤ")
//        NLogger.d(
//            TAG,
//            "打印奇怪怪怪的空格：",
//            ' '.hashCode(),
//            ' '.hashCode(),
//            '\u00A0'.hashCode(),
//            'ㅤ'.hashCode()
//        )
//        NLogger.d(TAG, "打印奇怪怪怪的空格，比较NBSP和正常空格：", ' ' == ' ')
//        NLogger.d(TAG, "打印奇怪怪怪的空格，比较NBSP和u00A0：", ' ' == '\u00A0')
//        NLogger.d(TAG, "打印奇怪怪怪的空格，比较u00A0和", '\u00A0' == 'ㅤ')
//        NLogger.d(TAG, "打印奇怪怪怪的空格，比较正常空格符和ㅤ", ' ' == 'ㅤ')

        "".replace("\\u00A0".toRegex(), " ")

        binding.fab.setOnClickListener { view ->
            ToastUtils.showLong("开始评测")
            val rBtn = binding.root.findViewById<RadioButton>(binding.rGroup.checkedRadioButtonId)
//            val entity =  EvalEntity(binding.editText.text.toString(),rBtn.hint.toString(),"http://track.benewtech.cn/NxVtwXWyIzTBgXlk13kDOXYI1g8=/FgkbfjBrr-49sstgYMEKXOCbYWaZ")
//            entity.autoClose = false
//            entity.beforeOrigin = false
//            entity.audioStart = 0
//            entity.audioEnd = 3000
//            EvalManager.startEval(entity)

            val entity = EvalSDKEntity(
                binding.editText.text.toString(),
                EvalType.ZH_CHS,
            )
            EvalSDK.startEval(entity)


        }

        binding.fabTop.setOnClickListener { view ->
            ToastUtils.showLong("初始化混合模式")

            Thread{
                EvalSDK.init(
                    this, EvalSDKConfig.Builder()
                        .setDeviceId("1103695934")
                        .setHost("https://gw.benewtech.cn/")
                        .setToken("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ2UktxbnBRYlVjbXB0UUtWSG8xaTZCSHNmSWo1aEQ4SXg5c3BYeDVBSVZ1eUNiYWoiLCJzIjpbImRldmljZV8xMTAzNjk1OTM0Il0sImFwcElkIjoibnR0LW0xIiwiZXhwIjoxNjc4NDE2ODgwLCJpYXQiOjE2NDYyOTk3NjZ9.oTFijR-JCABLGQb3Foug1XDHVOJbfXmgohpCfwUUt5E")
                        .setPlatform(EvalSDKPlatform.XS)
                        .setEvalSDKType(EvalSDKType.AUTO)
                        .build())
            }.start()
//            EvalSDK.deleteEval()

//            val pcmToWavUtil = classLoader.loadClass("com.xs.record.XSAudioRecorder")
//            val field = pcmToWavUtil.getDeclaredField("mExecutorService")
//            field.isAccessible = true
//            val get = field.get(XSAudioRecorder.getInstance()) as ExecutorService
//            Log.e(TAG, "onCreate: " + field)
//            EvalSDK.cancelEval()
//            get.submit {
//                Log.e(TAG, "onCreate: 执行线程")
//
//            }
//            get.awaitTermination(10,TimeUnit.SECONDS)
//
//            val executorService = classLoader.loadClass("java.util.concurrent.ExecutorService")
//            val declaredMethod = executorService.getDeclaredMethod("shutdownNow")
//            Log.e(TAG, "onCreate: "+declaredMethod)
//            declaredMethod.invoke(field,null)
            Log.e(TAG, "onCreate: 调用成功")


//            val mExecutorService = Executors.newSingleThreadExecutor()
//            mExecutorService.shutdownNow()
        }

        binding.fabTopStart.setOnClickListener { view ->
            ToastUtils.showLong("初始化在线模式")

            Thread{
                EvalSDK.init(
                    this, EvalSDKConfig.Builder()
                        .setDeviceId("1103695934")
                        .setHost("https://gw.benewtech.cn/")
                        .setToken("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ2UktxbnBRYlVjbXB0UUtWSG8xaTZCSHNmSWo1aEQ4SXg5c3BYeDVBSVZ1eUNiYWoiLCJzIjpbImRldmljZV8xMTAzNjk1OTM0Il0sImFwcElkIjoibnR0LW0xIiwiZXhwIjoxNjc4NDE2ODgwLCJpYXQiOjE2NDYyOTk3NjZ9.oTFijR-JCABLGQb3Foug1XDHVOJbfXmgohpCfwUUt5E")
                        .setPlatform(EvalSDKPlatform.XS)
                        .setEvalSDKType(EvalSDKType.CLOUD)
                        .build())
            }.start()
        }

        binding.fabBottomStart.setOnClickListener { view ->
            ToastUtils.showLong("delete")
            EvalSDK.deleteEval()
        }
        EvalSDK.registerEvalListener(object :IEvalSDKListener{
            override fun onReady() {
                super.onReady()
                ToastUtils.showLong("初始化完成")
            }
        })
    }

}